﻿
namespace Patterns4Net.Core.CecilExtensions
{
    using System.Diagnostics.Contracts;
    using Mono.Cecil;

    public class FieldAccess
    {
        public FieldAccess(FieldReference field, StackItem assignedValue)
        {
            this.Field = field;
            this.AssignedValue = assignedValue;
        }

        public FieldReference Field { get; private set; }

        public StackItem AssignedValue { get; private set; }

        public bool IsRead
        {
            get
            {
                return this.AssignedValue == null;
            }
        }

        public bool IsWrite
        {
            get
            {
                return this.AssignedValue != null;
            }
        }

        [ContractInvariantMethod]
        private void Invariant()
        {
            // this.IsWrite -> this.AssignedValue != null
            Contract.Invariant(this.IsWrite == false || this.AssignedValue != null);
            Contract.Invariant(this.IsRead != this.IsWrite);
        }
    }
}
